package com.atguigu.flink.sql;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * Created by Smexy on 2023/2/5
 */
public class Demo3_ReadKafka
{
    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        StreamTableEnvironment tableEnvironment = StreamTableEnvironment.create(env);

        /*
            定义一张表，这张表映射kafka的某个主题

                数据类型参考: https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/dev/table/types/
         */
        String createTableSql = "create table t1( id string,ts bigint ,vc int ) with(" +
            "                    'connector' = 'kafka' ," +
            "                    'topic' = 'topicA' , " +
            "                    'properties.bootstrap.servers' = 'hadoop102:9092' , " +
            "                    'properties.group.id' = 'test3' , " +
            "                    'scan.startup.mode' = 'earliest-offset' , " +
            "                    'value.format' = 'json'  " +
            "                    )";

        //执行建表(写操作)
        tableEnvironment.executeSql(createTableSql);

        //执行查询
        tableEnvironment.sqlQuery("select * from t1")
                        .execute()
                        .print();

    }
}
